package com.jsr.demo.controller;

import com.jsr.demo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserController
 * @description: TODO
 * @author: 朱同学
 * @Date 2021/4/24 21:26
 **/
@Slf4j
@RestController()
public class UserController {

    @GetMapping("/hello")
    public String hello() {

        return "hello world!";
    }

    @PostMapping("/add")
    public Map save(@Valid User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            Map<String, String> map = new HashMap<>();

            bindingResult.getFieldErrors().forEach((item) -> {
                String message = item.getDefaultMessage();
                String field = item.getField();
                map.put(field, message);
            });
            //return Result.build( 400 , "非法参数 !" , map);
            return map;
        }
        //return Result.ok();
        return (Map) new HashMap().put("code", "200");
    }

}
